<?php
require_once APP . '/application/controllers/FrontAction.php';
require_once APP . '/application/models/Article.php';

/**
 * 访问学者
 */
class FwxzController extends FrontAction
{
    
	public function init()
	{
		$this->view->typename = "Scolars";
	}
    /**
     * 首页
     *
     */
    public function indexAction()
    {
    	// empty
    }
    

    /**
     * 列表页面
     *
     */
    public function listAction()
    {
    	$id = (int)$_REQUEST['id'];
    	 
    	$nowp = intval($this->getParam("nowp", 1));
    	 
    	// 读取文章列表
    	$obj_article = new Article($this->db);
    	$result = $obj_article->listArticle($id, $nowp, 15);
    	
    	// 读取特殊列表
    	$arr_author = $this->db->fetchAll("select aid,title from article where cid={$this->cfg['fwxz_xz_type']} order by seq desc,aid desc");
    	$this->view->authors = $arr_author; 
    	
    	// 如果分类为子分类，则直接显示
    	if ($id == $this->cfg['fwxz_xz_type']){
    		if (count($arr_author) > 0){
    			$_REQUEST['id'] = $arr_author[0]['aid'];
    			return $this->showAction();
    		}
    	}
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->pagenum = Util::pageNumStyle($result['page']);

    	$this->view->typeid = $id;
    	$this->view->cname = $this->getParam("c");
    	$this->view->htitle = $this->view->typename;
    	
    	$this->render("list");
    }
    
    /**
     * 读取相关新闻
     *
     */
    public function relAction()
    {
    	$id = (int)$_REQUEST['id'];
    	
    	// 读取文章列表
    	$obj_article = new Article($this->db);
    	$arr_articleinfo = $obj_article->show($id);
    	
    	$keywords = $arr_articleinfo['title'];
    	$arr_rel = array();
    	// 读取相关新闻
    	if (!empty($keywords)){
    		$keywords = $this->db->quote($keywords);
    		$arr_rel = $this->db->fetchAll("select aid,cid,title from article where aid!={$arr_articleinfo['aid']} and content like '%{$keywords}%' order by seq desc,aid desc limit 5");
    		
    		$arr_articles = array();
	        foreach ($this->view->types['zxgk'] as $v){
	        	$v['c'] = 'zxgk';
	        	$arr_articles[$v['id']] = $v;
	        }
	        foreach ($this->view->types['zzjg'] as $v){
	        	$v['c'] = 'zzjg';
	        	$arr_articles[$v['id']] = $v;
	        }
	        foreach ($this->view->types['fwxz'] as $v){
	        	$v['c'] = 'fwxz';
	        	$arr_articles[$v['id']] = $v;
	        }
	        foreach($this->cfg['article_types'] as $v){
	        	$v['c'] = 'article';
	        	$arr_articles[$v['id']] = $v;
	        }
	        
	        foreach ($arr_rel as $key => $v){
	        	if (!isset($arr_articles[$v['cid']]))continue;
	    		$arr_rel[$key]['url'] = $this->url($this->view->baseUrl, $arr_articles[$v['cid']]['c'], "show", "id=" . $v['aid']);
	        }
    	}
    	
    	echo json_encode($arr_rel);
    }
    
    /**
     * 显示页面
     *
     */
    public function showAction()
    {
    	$id = (int)$_REQUEST['id'];
    	
    	$nowp = intval($this->getParam("nowp", 1));
    	
    	// 读取文章列表
    	$obj_article = new Article($this->db);
    	$arr_articleinfo = $obj_article->show($id);
    	$cid = $arr_articleinfo['cid'];
    	
    	// 读取特殊列表
    	$arr_author = $this->db->fetchAll("select aid,title from article where cid={$this->cfg['fwxz_xz_type']} order by seq desc,aid desc");
    	$this->view->authors = $arr_author;
    	
    	
    	
    	$this->view->typeid = $cid;
    	$this->view->cname = $this->getParam("c");
    	$this->view->content = $arr_articleinfo;
    	$this->view->htitle = htmlspecialchars($arr_articleinfo['title']);
    	
    	$this->render("show");    	 
    }
}
